package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.Car;
import com.ruoyi.system.mapper.CarMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectMapper;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.service.IProjectService;

/**
 * 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-24
 */
@Service
public class ProjectServiceImpl implements IProjectService
{
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private CarMapper carMapper;

    /**
     * 查询项目
     */
    @Override
    public Project selectProjectById(String id)
    {
        return projectMapper.selectProjectById(id);
    }

    /**
     * 查询项目列表
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {
        List<Project> projects = projectMapper.selectProjectList(project);
        int i=0;
        //将项目名称改写成 年+月+项目名
        for(Project item:projects){
            SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
            SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
            String year = yearFormat.format(item.getStartTime());
            String month = monthFormat.format(item.getStartTime());
            String result = year + month;
            item.setName(result+item.getName());
            //设置序号
            item.setPid((i++) + 1);
            //统计项目进度
            String process = String.valueOf(projectMapper.queryProcess(item.getId()))+'%';
            item.setProcess(process);
            projectMapper.saveProcess(item.getId(),item.getProcess());
        }
        return projects;
    }

    /**
     * 获取所有项目名称
     */
    @Override
    public List<Project> getProjectNames() {
        return projectMapper.getProjectNames();
    }

    /**
     * 新增项目
     */
    @Override
    public int insertProject(Project project) {
        //先创建项目 才能创建car
        //生成uuid
        String uuid = IdUtils.fastSimpleUUID();
        project.setId(uuid);
        int result = projectMapper.insertProject(project);
        //获取所有车牌和车型 插入数据库
        List<Car> cars = project.getCars();
        for (Car car:cars){
            car.setProjectId(uuid);
            car.setStatus("0");
            car.setIsFound("0");
            carMapper.insertCar(car);
        }
        return result;
    }

    /**
     * 修改项目
     */
    @Override
    public int updateProject(Project project)
    {
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目
     */
    @Override
    public int deleteProjectByIds(String[] ids) {
        //删除之前先查询该项目下是否有车辆
        for(String id:ids){
            Car car = new Car();
            car.setProjectId(id);
            List<Car> cars = carMapper.selectCarList(car);
            //如果不为空则说明当前项目下还有车辆 不能删除
            if(cars.size() != 0){
                return 0;
            }
        }
        return projectMapper.deleteProjectByIds(ids);
    }

    /**
     * 删除项目信息
     */
    @Override
    public int deleteProjectById(String id) {
        return projectMapper.deleteProjectById(id);
    }
}
